package com.windoent.yugioh.ui.activity.registlogin;

import android.view.View;
import android.widget.TextView;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.GuiZeHtmlBean;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import net.nightwhistler.htmlspanner.HtmlSpanner;

import org.json.JSONException;

/**
 * author : robert
 * date   : 2021/6/4 11:19
 * desc   :用户注册协议
 */
public class RegistPrivacyActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private TextView mTvContent;

    @Override
    public int getLayout() {
        return R.layout.activity_privacy_regist;
    }

    @Override
    public void initView() {
        super.initView();
        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mTvContent = (TextView) findViewById(R.id.tv_content);
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    //规则
    private void guizeHtml() {
        showLoadingDialog("");
        PhoneLive.guiZeHtml("2", new StringCallback() {
            @Override
            public void onError(okhttp3.Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            GuiZeHtmlBean json = gson.fromJson(response, GuiZeHtmlBean.class);
                            if (json != null) {
                                GuiZeHtmlBean.DataBean data = json.getData();
                                if (data != null) {
                                    String content = data.getContent();
                                    if (BaseMethod.notNull(content)) {
                                        mTvContent.setText(new HtmlSpanner().fromHtml(content));
                                    }
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "用户注册协议页面");
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "用户注册协议页面");
        guizeHtml();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }
}
